package com.cn.utils;

import org.springframework.util.StringUtils;

import javax.persistence.Transient;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/5/3.
 */
public class StringUtil {
//    private int [] ii = {0,1,2,3,4,5,6,7,8,9};
    static Random random = new Random();

    public static Long getRandom5(){
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<5;i++){
            sb.append(random.nextInt(10));
        }

        return Long.parseLong(sb.toString());
    }

    /**
     * 字符串补0
     * @param str
     * @param size
     * @return
     */
    public static String toStr(String str, int size) {
        int s_length = (str == null ? 0 : str.length());
        size = size - s_length;
        StringBuilder sb = new StringBuilder();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                sb.append("0");
            }
        }
        sb.append(str == null ? "" : str);
        return sb.toString();
    }

    public static String trimAround(String str){
        int index1=0;
        int index2=str.length();
        if(str.charAt(0)==','){
            index1=1;
        }
        if(str.charAt(index2-1)==','){
            index2=str.length()-1;
        }
        return str.substring(index1,index2);
    }

    /**
     * sign 只能一位字符
     * @param list
     * @param sign
     * @return
     */
    public static String mergeStr(Object [] list,String  sign){
        if(list==null){
            return null;
        }
        if(sign==null||sign.length()>1){
            return null;
        }
        StringBuffer sb=new StringBuffer("");
        for(Object s : list){
            if(s!=null&&!s.equals("")){
                sb.append(s).append(sign);
            }
        }
        if(sb.length()>0){
            sb.deleteCharAt(sb.length()-1);
        }
       return sb.toString();
    }

    public static String noZerofill(String str) {
        int s_length = (str == null ? 0 : str.length());
        int index=0;
        if (s_length > 0) {
            for (int i = 0; i < s_length; i++) {
               if(str.charAt(i)!='0'){
                   index=i;
                   break;
               }
            }
        }
        String strLast = str.substring(index, s_length);// 截取字符串
        return strLast;
    }
    public static boolean isEmpty(Object obj) {
        if (!isNotempty(obj)) {
            return true;
        }
        return false;
    }

    public static boolean isNotempty(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof List) {
            List list = (List) obj;
            return list.size() > 0;
        } else {
            return obj.toString().trim().length() > 0;
        }
    }

    public static String toLimitOffSet(Integer pageSize, Integer pageNumber) {
        pageSize = (pageSize == null) ? 10 : pageSize;
        pageNumber = (pageNumber == null || pageNumber <= 0) ? 1 : pageNumber;
        return "limit " + pageSize + " OFFSET " + ((pageNumber - 1) * pageSize);
    }

    public static String toLimit(Integer pageSize, Integer pageNumber) {
        pageSize = (pageSize == null) ? 10 : pageSize;
        pageNumber = (pageNumber == null || pageNumber <= 0) ? 1 : pageNumber;
        return "limit " + ((pageNumber - 1) * pageSize) + "," + pageSize;
    }

    public static Object[] getObj(Object cp, String type) {
        List<Object> list = new ArrayList<Object>();

        Field[] fs = cp.getClass().getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            f.setAccessible(true); //设置些属性是可以访问的
            Transient tran = f.getAnnotation(Transient.class);
            if (tran != null) {
                continue;
            }
            Object val = null;//得到此属性的值
            try {
                val = f.get(cp);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            list.add(val);
        }
        return list.toArray();
    }


    public static List<String> getDateList(String startDate, String endDate, String type) throws ParseException {
        return getDateList(startDate,endDate,type,null);
    }
    public static List<String> getDateList(String startDate, String endDate, String type, String pattern) throws ParseException {
        List<String> list = new ArrayList<String>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long start = simpleDateFormat.parse(startDate).getTime();
        long end = simpleDateFormat.parse(endDate).getTime();
        long result = end - start;
        long time = 60 * 60 * 1000;
        long day = time * 24;

        if (result >= day) {
            long j = result / day;
            long m = result % day;
            long nowDay = strToTime(startDate);
            for (int i = 0; i <= j; i++) {
                addList(list, nowDay, type,pattern);
                nowDay += day;
            }
        } else if (result < day & result > 0) {
            long j = result / time;
            long m = result % time;
            long nowTime = strToTime(startDate, StringUtils.isEmpty(pattern) ?"yyyy-MM-dd HH":pattern);
            for (int i = 0; i <= j; i++) {
                addList(list, nowTime, type,pattern);
                nowTime += time;
            }
        }
        return list;
    }

    /**
     * time 就是 时间戳类型
     *
     * @param list
     * @param nowTime
     * @param type
     */
    public static void addList(List<String> list, long nowTime, String type,String pattern) {
        if (type == "time") {
            list.add(nowTime + "");
        } else {
            list.add(timeToStr(nowTime,  StringUtils.isEmpty(pattern) ?"yyyyMMddHH":pattern ));
        }
    }

    /**
     * 转成当天开始时间的时间戳 如 2017-05-24 11:57:45 --> 2017-05-24 00:00:00 --> 1495555200000
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static long strToTime(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date d = simpleDateFormat.parse(date);
        return d.getTime();
    }

    public static long strToTime(String date, String pattern) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date d = simpleDateFormat.parse(date);
        return d.getTime();
    }

    public static String timeToStr(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    public static String timeToStr(long time, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    public static String getChangeDataStr(long time) {
        return getChangeDataStr(time,"yyyy-MM-dd-HH:mm:ss");
    }
    //"yyyy-MM-dd-HH-mm-ss-SSS"
    public static String getChangeDataStr(long time,String pattern) {
        StringBuilder dataStr = new StringBuilder();
        String[] stime = timeToStr(time, pattern).split("-");
        int i = 0;
        long num=0;
        for (String s : stime) {
             num = Long.parseLong(s);
            i++;
            calculation(dataStr,num,i);
//            dataStr.append();
        }
        return dataStr.toString();
    }

    public static String getChangeData(String val) {
        StringBuilder dataStr = new StringBuilder();
        long num=0;
        for (char s : val.toCharArray()) {
            num = Character.getNumericValue(s);
            dataStr.append(9-num);
        }
        return dataStr.toString();
    }
    public static String changeDataStr(String str) throws ParseException {
       return timeToStr(strToTime(getChangeData(str),"yyyyMMddHHmmssSSS"));
    }
    public static Long changeDataTime(String str) throws ParseException {
        return strToTime(getChangeData(str),"yyyyMMddHHmmssSSS");
    }

    public static void calculation( StringBuilder str, long num, int size) {
        if (size >= 1 & size <= 7) {
//            StringBuffer str = new StringBuffer();
            switch (size) {
                case 1:
                    str.append(9999 - num);
                    break;
                case 2:
                    str.append(99 - num);
                    break;
                case 3:
                    str.append(99 - num);
                    break;
                case 4:
                    str.append(99 - num);
                    break;
                case 5:
                    str.append(99 - num);
                    break;
                case 6:
                    str.append(99 - num);
                    break;
                case 7:
                    Long l = 999 - num;
//                System.out.println(l);
                    str.append(toStr(l.toString(), 3));
                    break;
            }
//            return str.toString();
        }
//        return null;
    }

    public boolean matches(String pattern,String content){
        boolean isMatch = Pattern.matches(pattern, content);
        return isMatch;
    }
    /**
     * 手机号验证
     * @return 验证通过返回true
     */
    private static Pattern CHECK_PHONE_MOBILE=Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
    public static boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = CHECK_PHONE_MOBILE; // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }
    /**
     * 电话号码验证
     * @return 验证通过返回true
     */
    private static Pattern CHECK_PHONE=Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");
    private static Pattern CHECK_PHONE2=Pattern.compile("^[1-9]{1}[0-9]{5,8}$");
    public static boolean isPhone(final String str) {
        Pattern p1 = null, p2 = null;
        Matcher m = null;
        boolean b = false;
        p1 = CHECK_PHONE;  // 验证带区号的
        p2 = CHECK_PHONE2;         // 验证没有区号的
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        } else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;
    }

    public static boolean isPhon2(String phone) {
        String regex = "^[1][3456789][0-9]{9}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    public static boolean isZhongWen(String str){
        if(str!=null){
            String regex="^[\u4e00-\u9fa5]";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(str);
            boolean isMatch = m.matches();
            return isMatch;
        }
        return false;
    }
    /**
     *
     * @param key  运营商
     * @param jiFeiShiChang 计费时长
     * @param total  所打电话总数
     * @param isChangTu  是否长途
     * @return
     */
    public static long meterCost(String key,long jiFeiShiChang,long total,boolean isChangTu){
        if(jiFeiShiChang==0){
            return 0;
        }
        if("dongJin-".endsWith(key)){
            return dongJinMeterCost(jiFeiShiChang,total,isChangTu);
        }else if("aliCloud-".equals(key)){
            return aliCloudMeterCost(jiFeiShiChang,total,isChangTu);
        }
        return 0;
    }
    public static long dongJinMeterCost(long jiFeiShiChang,long total,boolean isChangTu){
        long n=0;
        if(isChangTu){
            n=(jiFeiShiChang/60)*300;
        }else{
            n=(jiFeiShiChang/60)*100;
        }
        return n;
    }

    public static long aliCloudMeterCost(long jiFeiShiChang, long total, boolean isChangTu) {
//        long n=0;
        long wan = 10000;
        long price = 55;
        if (total <= (5 * wan)) { //小于 5W
            price = priceChange(isChangTu, 55, 110);
        } else if (total > (5 * wan) && total <= (10 * wan)) { //5w < total <= 10W
            price = priceChange(isChangTu, 54, 108);
        } else if (total > (10 * wan) && total <= (50 * wan)) {  //10w < total <= 50W
            price = priceChange(isChangTu, 52, 104);
        } else if (total > (50 * wan) && total <= (100 * wan)) {  //50w < total <= 100W
            price = priceChange(isChangTu, 50, 100);
        } else if (total > (100 * wan) && total <= (500 * wan)) {  //100w < total <= 500W
            price = priceChange(isChangTu, 48, 96);
        } else if (total > (500 * wan) && total <= (1000 * wan)) {  //500w < total <= 1000W
            price = priceChange(isChangTu, 46, 92);
        } else if (total > (1000 * wan)) {                       // total > 1000W
            price = priceChange(isChangTu, 44, 88);
        }
//        n=(jiFeiShiChang/60)*price;
        return (jiFeiShiChang / 60) * price;
    }
    private static long priceChange(boolean isChangTu,long priceLocal,long price){
        if(isChangTu){
            return price;
        }
        return priceLocal;
    }
//    private final static String dongJin="dongJin-";
//    private final static String aliCloud="aliCloud-";

    public static TimeSlot getTime(Long time,int offer) throws ParseException {
        String nowTime=timeToStr(System.currentTimeMillis(),"yyyy-MM-dd");
        Long startTime = StringUtil.strToTime(nowTime+" 00:00:00:000","yyyy-MM-dd HH:mm:ss:SSS");
//        Long endTime = StringUtil.strToTime(nowTime+" 23:59:59:999","yyyy-MM-dd HH:mm:ss:SSS");
        long timeTotal=24*60*60*1000;
        long num=24*12;
        long timeNum=timeTotal/num;
        List<TimeSlot> list = getTimeList(num,timeNum,startTime,-4);
        for(int i=0;i<list.size();i++){
            if(time>=list.get(i).getStartTime()&&time<list.get(i).getEndTime()){
                return list.get(i-offer);
            }
        }
        return null;
    }

    public static List<TimeSlot> getTimeList(long num,long timeNum,long startTime,int loop){
        List<TimeSlot> list= new ArrayList<>();
        long startTimeParam=0;
        long endTimeParam=0;
        for(int i=loop;i<=num;i++){
            startTimeParam=startTime+((i-1)*timeNum);
            endTimeParam=startTime+(i*timeNum);
            list.add(new TimeSlot(startTimeParam,endTimeParam));
        }
        return list;
    }


    public static void main(String[] args) throws ParseException, InterruptedException {

        System.out.println(getRandom5());

        String sss="123123134";
        System.out.println(noZerofill("0005406304"));

        System.out.println(trimAround(",1,2,3"));
        long time = StringUtil.strToTime("2018-06-25","yyyy-MM-dd");
        System.out.println(StringUtil.getChangeDataStr(time,"yyyy-MM-dd"));

//        long now =strToTime("2017-10-30 00:02:17");
//        TimeSlot t = getTime(now,2);

//        System.out.println(changeDataStr("79829078996891853"));
//        System.out.println(timeToStr(t.getStartTime())+" -- "+timeToStr(t.getEndTime()));

       /* System.out.println(aliCloudMeterCost(60,50000,false)+"--"+aliCloudMeterCost(60,50000,true));
        System.out.println(aliCloudMeterCost(120,50000,false)+"--"+aliCloudMeterCost(120,50000,true));

        System.out.println(aliCloudMeterCost(60,80000,false)+"--"+aliCloudMeterCost(60,80000,true));
        System.out.println(aliCloudMeterCost(120,80000,false)+"--"+aliCloudMeterCost(120,80000,true));

        System.out.println(aliCloudMeterCost(60,250000,false)+"--"+aliCloudMeterCost(60,250000,true));
        System.out.println(aliCloudMeterCost(120,250000,false)+"--"+aliCloudMeterCost(120,250000,true));

        System.out.println(aliCloudMeterCost(60,750000,false)+"--"+aliCloudMeterCost(60,750000,true));
        System.out.println(aliCloudMeterCost(120,750000,false)+"--"+aliCloudMeterCost(120,750000,true));

        System.out.println(aliCloudMeterCost(60,1250000,false)+"--"+aliCloudMeterCost(60,1250000,true));
        System.out.println(aliCloudMeterCost(120,1250000,false)+"--"+aliCloudMeterCost(120,1250000,true));

        System.out.println(aliCloudMeterCost(60,7500000,false)+"--"+aliCloudMeterCost(60,7500000,true));
        System.out.println(aliCloudMeterCost(120,7500000,false)+"--"+aliCloudMeterCost(120,7500000,true));

        System.out.println(aliCloudMeterCost(60,12000000,false)+"--"+aliCloudMeterCost(60,12000000,true));
        System.out.println(aliCloudMeterCost(120,12000000,false)+"--"+aliCloudMeterCost(120,12000000,true));*/

//        System.out.println(strToTime("2017-05-27 10:00:00"));
//        System.out.println(strToTime("2017-05-27 10:00:00","yyyy-MM-dd HH"));
//        System.out.println(strToTime("2017-05-27 11:00:00","yyyy-MM-dd HH"));
//        System.out.println(strToTime("2017-05-27 12:00:00","yyyy-MM-dd HH"));
//        System.out.println(strToTime("2017-05-27 13:00:00","yyyy-MM-dd HH"));
//        System.out.println(strToTime("2017-05-27 14:00:00","yyyy-MM-dd HH"));

//        String newTime=StringUtils.timeToStr(System.currentTimeMillis(),"yyyyMMddHHmmssSSS");


//        String ss = "1495866950353c-000000000000000house-u043677";
//        System.out.println(ss.length());
//        System.out.println(timeToStr(1495555210101L));
//        System.out.println(System.currentTimeMillis());
//        List<String> list = getDateList("2017-06-20 00:08:07", "2018-01-24 14:21:00", "");
        List<String> slist = StringUtil.getDateList("2017-01-01 00:00:00", "2018-06-13 00:00:00", "","yyyyMMdd");
        for (String s : slist) {
            System.out.println(s);
        }
//        1495864800000
//        1495866950353


//        System.out.println(getChangeDataStr(System.currentTimeMillis()) );
//        Thread.sleep(100);
//        System.out.println(getChangeDataStr(System.currentTimeMillis()) );
        long timeStart = System.currentTimeMillis();
//        for (int i = 0; i < 1000000; i++) {
//            isPhone(sss);
//            long time = System.currentTimeMillis();
//            System.out.println(timeToStr(time, "yyyyMMddHHmmss-SSS"));
//            System.out.println(getChangeDataStr(time));
//            getChangeDataStr(time,"yyyy-MM-dd-HH-mm-ss");
//            UUID.randomUUID().toString();
//            Thread.sleep(100);
//            System.out.println("========================================");
//        }
        System.out.println(System.currentTimeMillis()-timeStart);
    }
}
